/**
 * 
 */
package com.stylefeng.guns.rest.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
public class JsonUtil {

	/**
	 * 对象转成MAP
	 * 
	 * @param c
	 * @return
	 */
	public static Map<String, Object> bean2Map(Object c) {
		return json2Map(JSON.toJSONString(c));
	}

	
	@SuppressWarnings("unchecked")
	public static Map<String, Object> json2Map(String jsonStr) {
		if (jsonStr==null || jsonStr.trim().equals("")) {
			return new HashMap<String, Object>();
		}
		Map<String, Object> map = (Map<String, Object>) JSON.parse(jsonStr);
		return map;
	}
	
	public static List<Map<String, Object>> json2ListMap(String jsonStr){
		List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
		if (jsonStr==null || jsonStr.trim().equals("")) {
			return data;
		}
		JSONArray array = JSON.parseArray(jsonStr);
		for (int i = 0; i < array.size(); i++) {
			data.add(json2Map(JSON.toJSONString(array.get(i))));
		}
		return data;
	}


	/**
	 * @return
	 */
	public static String obj2String(Object obj) {
		return JSON.toJSONString(obj);
	}
	
	public static void main(String[] args) {
		 bean2Map("{\"customerPrices\":[],\"levelPrices\":[]}");
	}
}
